import { glob } from 'glob'
import fsp from 'fs/promises'
import axios from 'axios'
import https from 'https'

// 设置全局默认值
axios.defaults.httpsAgent = new https.Agent({
  rejectUnauthorized: false,
})

class Deduplication {
  constructor(data) {
    this.data = data
    this.jsons = []
  }

  async start() {
    await this.ready()

    let files = await glob('D:/研判知识库/提取结果/papers/**/*.json')

    for (let i = 0; i < files.length; i++) {
      //   if (i >= 5) break

      const file = files[i]
      const data = await fsp.readFile(file, 'utf-8')
      let json

      try {
        json = JSON.parse(data)
      } catch (e) {
        console.error('Error parsing JSON:', e)
        continue
      }

      for (let j = 0; j < json.length; j++) {
        const item = json[j]
        await this.handleAttackData(item, file, j)
      }

      console.log('进度', i + 1, '/', files.length)
    }

    const jsonlContent = this.jsons.map(obj => JSON.stringify(obj)).join('\n')
    await fsp.writeFile('papers.jsonl', jsonlContent)
  }

  async handleAttackData(data, filename, j) {
    try {
      const think = data.cot
      const input = data.theory + '\n'

      if (!data.attck_step || data.attck_step?.length == 0) return
      if (!data.detection || data.detection?.length == 0) return
      if (!data.mitigations || data.mitigations?.length == 0) return

      const attckStep =
        '攻击方案：\n' +
        data.attck_step
          .map((value, index, array) => {
            return `${index + 1}. ${value}`
          })
          .join('\n')

      const detection =
        '检测方案：\n' +
        data.detection
          .map((value, index, array) => {
            return `${index + 1}. ${value}`
          })
          .join('\n')

      const mitigations =
        '缓解措施：\n' +
        data.mitigations
          .map((value, index, array) => {
            return `${index + 1}. ${value}`
          })
          .join('\n')

      const theory = {
        input: `攻击原理：${input}`,
        output: `<think>\n${think}\n</think>\n${attckStep}${detection}${mitigations}`,
      }

      const res = await this.query(theory.input)
      const { file_name, score } = res
      console.log('score', score)

      // if (score >= 0.9) {
      //   console.log('大于0.85', {
      //     输入内容: theory.input,
      //     输入文件名: filename,
      //     输入内容下标: j,

      //     匹配到: file_name,
      //   })
      // }

      if (score >= 0.9) {
        const [targetFile, targetIndex] = file_name.split('.json-')
        const tartgetData = await fsp.readFile(targetFile + '.json', 'utf-8')
        const targetJson = JSON.parse(tartgetData)

        const filename =
          '../data/papers过滤后/' + Date.now() + '_' + Math.random().toString(36).substr(2, 9) + '.json'
        fsp.writeFile(
          filename,
          JSON.stringify({
            当前内容: data,
            匹配到: targetJson[targetIndex],
          })
        )
        return
      }

      await this.setData(filename + `-${j}`, JSON.stringify(theory.input))
      this.jsons.push(theory)
    } catch (e) {
      console.error('Error handling attack data:', e)
      //   throw e
    }
  }

  async ready() {
    try {
      await axios.delete('https://10.254.179.149:30444/v1/delete', {})
    } catch (error) {
      console.error('Error setting data:', error)
      throw error
    }
  }

  async setData(fileName, text) {
    try {
      await axios.post('https://10.254.179.149:30444/v1/text', {
        text,
        file_name: fileName,
      })
    } catch (error) {
      console.error('Error setting data:', error)
      throw error
    }
  }
  async query(text) {
    try {
      return await axios
        .post('https://10.254.179.149:30444/v1/score', {
          text,
        })
        .then(res => {
          return res.data
        })
    } catch (error) {
      console.error('Error querying score:', error)
      throw error
    }
  }
}

async function main() {
  const deduplication = new Deduplication()
  await deduplication.start()
}

main()
